﻿using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RK.SerivesPlugin.BPW
{
    [Description("销售订单审核时，反写日期和备注信息,审核前校验出口订单是否有单价"), HotUpdate]
    public class SalOrderAudit : AbstractOperationServicePlugIn
    {
        public override void BeforeExecuteOperationTransaction(BeforeExecuteOperationTransaction e)
        {
            base.BeforeExecuteOperationTransaction(e);
            //审核前，出口订单的含税单价小于等于0，则不允许审核
            foreach (var obj in e.SelectedRows.Select(s => s.DataEntity))
            {
                if (obj == null) { return; }
                DynamicObject billType = obj["BillTypeId"] as DynamicObject;
                string billTypeNumber = billType == null ? "" : billType["Number"].ToString();
                DynamicObjectCollection rows = obj["SaleOrderEntry"] as DynamicObjectCollection;
                foreach (var row in rows)
                {
                    decimal taxPrice = Convert.ToDecimal(row["TaxPrice"]);
                    if (billTypeNumber == "XSDD02_BPW" && taxPrice <= 0)
                    {
                        e.Cancel = true;
                        Base.AddOperationResult(this.OperationResult, false, "含税单价必须大于0", MessageType.FatalError, "含税单价必须大于0", 0);
                    }
                }
            }
        }
        public override void AfterExecuteOperationTransaction(AfterExecuteOperationTransaction e)
        {
            base.AfterExecuteOperationTransaction(e);
            var obj = e.DataEntitys[0];
            string billNo = obj["BillNo"].ToString();
            ToUpInfo(billNo);
            ToUpPlanDate(billNo);
        }
        /// <summary>
        /// 根据销售订单行内码联查出确认单后，把确认单的客户可到货日期反写到销售订单的要货日期
        /// </summary>
        /// <param name="billNo"></param>
        private void ToUpPlanDate(string billNo)
        {
            string str = string.Format($@"/*dialect*/ UPDATE T01
              SET 
                  T01.FDELIVERYDATE = T2.F_XRIS_AOGDATE
            FROM T_SAL_ORDERENTRY T0
                 JOIN T_SAL_ORDER T1 ON T1.FID = T0.FID
                 JOIN T_SAL_ORDERENTRY_D T01 ON T01.FENTRYID = T0.FENTRYID
                 JOIN T_PUR_SalConfirmEntry T2 ON T2.F_XRIS_SALENTRYID = T0.FENTRYID
            WHERE T1.FBILLNO = '{billNo}'");
            DBServiceHelper.Execute(this.Context, str);
        }
        /// <summary>
        /// 根据销售订单行内码联查出确认单后，把确认单的梅州可交货日期反写到销售订单的梅州交货日期和行备注
        /// </summary>
        /// <param name="billNo"></param>
        private void ToUpInfo(string billNo)
        {
            string str = string.Format($@"/*dialect*/ UPDATE T0
              SET 
                  T0.F_BPW_DELIVERYDATE = T2.F_XRIS_OKDATE, 
                  T0.FNOTE = ISNULL(T2.F_XRIS_REMARK, '')
            FROM T_SAL_ORDERENTRY T0
                 JOIN T_SAL_ORDER T1 ON T1.FID = T0.FID
                 JOIN T_PUR_SalConfirmEntry T2 ON T2.F_XRIS_SALENTRYID = T0.FENTRYID
            WHERE T1.FBILLNO = '{billNo}'");
            DBServiceHelper.Execute(this.Context, str);
        }
    }
}
